const _ = require('lodash');
const moment = require('moment');

module.exports = app => {
    const {router, middleware} = app;
    const prefix = '/api/event';


    /**操作记录列表*/
    router.get(prefix, middleware.api(2), async ctx => {
        const {limit, offset} = ctx.service.page.page();
        let [total, list] = await Promise.all([
            app.model.Event.count(),
            app.model.Event.findAll({
                order: [['id', 'desc']],
                limit,
                offset,
                include: [app.model.User.unscoped()],
            })
        ]);
        ctx.body = {list, total};
        ctx.body.list.forEach((item, i) => {
            ctx.body.list[i] = item = item.toJSON();
            item.created_at = moment(item.created_at).format('MM月DD日 HH点mm分ss秒');
        });
    });

    /**清空操作记录*/
    router.get(prefix + '/clear_event', middleware.api(2), async ctx => {
        await app.model.Event.destroy({truncate: true});
        ctx.service.event.add('删除所有用户操作记录');
        ctx.body = '清理完成';
    });
};